﻿using System;


using System.Text;
using IAM.Interface.Model.Interface;
using IAM.Interface.Model;
using System.Reflection;

namespace IAM.Interface.Model
{
    /// <summary>
    /// 单个用户的账号权限信息
    /// </summary>
    public class UserACI : AbstractConvertObject,IConvertXml
    {
        #region 属性
        private AccountInfo _accountinfo = null;
        private ACISessions _acisessions = null;
        /// <summary>
        /// 用户的账号信息
        /// </summary>
        public AccountInfo AccountInfo
        {
            get
            {
                return _accountinfo;
            }
            set
            {
                _accountinfo = value;
            }
        }
        /// <summary>
        /// 用户的权限描述信息
        /// </summary>
        public ACISessions ACISessions
        {
            get
            {
                return _acisessions;
            }
            set
            {
                _acisessions = value;
            }
        }
        #endregion
        #region IConvertXml

        public string XMLBoday
        {
            get
            {
                StringBuilder _xml = new StringBuilder();
                _xml.Append(" <UserACI>");
                _xml.Append(this.AccountInfo.XML);
                _xml.Append(this.ACISessions.XML);
                return _xml.ToString();
            }


        }

        public string XMLFinis
        {
            get
            {
                return "</UserACI>";
            }
        }
        public string XML
        {
            get
            {
                return XMLBoday + XMLFinis;
            }
        }
        #endregion
        #region AbstractConvertObject
        public override object ConvertToObject()
        {
            this.AccountInfo = new AccountInfo();
            this.AccountInfo.CurrentXmlNode = this.CurrentXmlNode.SelectSingleNode("AccountInfo");
            this.AccountInfo = (AccountInfo)this.AccountInfo.ConvertToObject();

            this.ACISessions = new ACISessions();
            this.ACISessions.CurrentXmlNode = this.CurrentXmlNode.SelectSingleNode("ACISessions");
            this.ACISessions = (ACISessions)this.ACISessions.ConvertToObject();
            return this;
        }
        #endregion
        #region 重写Equals
        /// <summary>
        /// 确定指定的 UserACI 对象是否等效于当前 UserACI
        /// 1.判断obj是否是UserACI类型
        /// 2.判断 AccountInfo属性是否一致
        /// 3.判断 ACISessions属性是否一致
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            UserACI ua = null;
            //1.判断obj是否是UserACI类型
            try
            {
                ua = (UserACI)obj;
            }
            catch
            {
                return false;
            }
            bool result = true;
            //2.判断 AccountInfo属性是否一致

            if (!this.AccountInfo.Equals(ua.AccountInfo))
            {
                result = false;
            }
            //3.判断 ACISessions属性是否一致
            else if(!this.ACISessions.Equals(ua.ACISessions))
            {
                result = false;
            }
            return result;
        }

        #endregion
        #region 重写GetHashCode
        /// <summary>
        /// 哈希函数
        /// 应为可能存在集合的顺序问题此函数不能作为对象的唯一标志
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            int result = 1;
            PropertyInfo[] pi = this.GetType().GetProperties();
            foreach (PropertyInfo onofpi in pi)
            {
                result = result + onofpi.GetHashCode();
            }
            return result;
        }
        #endregion
    }
}
